package com.wyg.composite.component;

import com.wyg.composite.duck.Quackable;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @description:    使用组合模式管理所有的鸭子
 * @author: WYG
 * @time: 2021/4/10 21:48
 */

public class Flock implements Quackable {
    private ArrayList<Quackable> quackers = new ArrayList<>();

    public void add(Quackable quackable) {
        quackers.add(quackable);
    }
    @Override
    public void quack() {
        // 迭代器模式
        Iterator iterator = quackers.iterator();
        while (iterator.hasNext()) {
            Quackable quackable = (Quackable) iterator.next();
            quackable.quack();
        }
    }
}
